package com.wngbms.netty._01simple.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

public class ClientHandlerImpl extends ChannelInboundHandlerAdapter {

    public ClientHandlerImpl() {
        System.out.println(Thread.currentThread().getName() + " ClientHandlerImpl");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String x = Thread.currentThread().getName();
        System.out.println(x + " channelActive");
        ctx.writeAndFlush(Unpooled.copiedBuffer("client send something", CharsetUtil.UTF_8));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        System.out.println(Thread.currentThread().getName() + " channelRead " +buf.toString(CharsetUtil.UTF_8));
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println(Thread.currentThread().getName() + " ReadComplete");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(Thread.currentThread().getName() + " exceptionCaught");
        cause.printStackTrace();
        ctx.close();
    }
}
